package com.doupi.partner.api.biz;

import com.doupi.partner.api.core.AbstractBiz;
import com.doupi.partner.api.core.ApiRequest;
import com.doupi.partner.api.core.ApiResponse;
import com.doupi.partner.api.core.Security;
import com.doupi.partner.constant.PartnerTypeEnum;
import com.doupi.partner.manager.PartnerRelationManager;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.service.PartnerRelationService;
import com.doupi.partner.util.AssertUtils;
import com.doupi.partner.util.ObjectUtils;
import com.doupi.partner.util.RedisUtils;
import com.doupi.partner.util.RuntimeBusinessException;
import com.doupi.partner.util.constant.UtilConstants;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 合伙人注册
 * 
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-04-29
 */
@Security
@Component("partnerRegister")
public class PartnerRegisterFacade extends AbstractBiz {

	private final PartnerRelationService partnerRelationService;

	private final PartnerRelationManager partnerRelationManager;

	private final RedisUtils redisUtils;

	@Autowired
	public PartnerRegisterFacade(PartnerRelationService partnerRelationService, PartnerRelationManager partnerRelationManager, RedisUtils redisUtils) {
		this.partnerRelationService = partnerRelationService;
		this.partnerRelationManager = partnerRelationManager;
		this.redisUtils = redisUtils;
	}

	@Override
	protected void execute(ApiRequest request, ApiResponse response) {
		String mobile = MapUtils.getString(request.getData(), "mobile");
		AssertUtils.assertNotNull(mobile, "手机号不能为空");
		String name = MapUtils.getString(request.getData(), "name");
		AssertUtils.assertNotNull(name, "姓名不能为空");
		String code = MapUtils.getString(request.getData(), "code");
		AssertUtils.assertNotNull(code, "短信验证码不能为空");
		String originCode = redisUtils.get(UtilConstants.IDENTIFY_CODE_CACHE_KEY_PREFIX + mobile);
		AssertUtils.assertEqual(originCode, code, UtilConstants.FAIL_STATUS_CODE, "验证码不正确");
		PartnerRelation partnerRelation = ObjectUtils.parseMapToObject(request.getData(), PartnerRelation.class);
		AssertUtils.assertNotNull(partnerRelation.getMarketingNumber(), "链接不正确");
		PartnerRelation superiorPartner = partnerRelationService.selectByMarketNumber(partnerRelation.getMarketingNumber());
		AssertUtils.assertNotNull(superiorPartner, "链接不正确");
		String password = MapUtils.getString(request.getData(), "password");
		AssertUtils.assertNotBlank(password, "密码不能为空");

		// TODO 提取
		partnerRelation.setMarketingNumber("1" + RandomStringUtils.random(9, "1234567890"));
		PartnerTypeEnum partnerTypeEnum = PartnerTypeEnum.getPartnerTypeByCode(superiorPartner.getType());
		AssertUtils.assertNotNull(partnerTypeEnum, "合伙人非法");
		switch (partnerTypeEnum) {
		case HEADQUARTERS:
		case SUBSTATION:
		case CITY:
			partnerRelation.setType(PartnerTypeEnum.SUPERIOR.getCode());
			partnerRelation.setCityAdministratorId(superiorPartner.getAdministratorId());
			break;
		case SECOND:
		case SUPERIOR:
			partnerRelation.setCityAdministratorId(superiorPartner.getCityAdministratorId());
			partnerRelation.setType(PartnerTypeEnum.SECOND.getCode());
			break;
		default:
			throw RuntimeBusinessException.createException("合伙人非法");
		}
		partnerRelation.setSuperiorAdministratorId(superiorPartner.getAdministratorId());
		partnerRelation.setSubstationAdministratorId(superiorPartner.getSubstationAdministratorId());
		partnerRelation.setCity(superiorPartner.getCity());
		partnerRelation.setNickname(name);

		int ret = partnerRelationManager.savePartnerRelation(mobile, name, partnerRelation, password);
		if (ret <= 0) {
			throw RuntimeBusinessException.createException("注册失败");
		}
	}
}
